package com.starmark.gateway.auth.provider.command;




import com.starmark.gateway.auth.provider.constants.GatewayAuthApiConstant;
import com.starmark.gateway.auth.provider.entity.GatewayAuthRole;
import com.starmark.gateway.auth.provider.entity.GatewayAuthRor;
import com.starmark.gateway.auth.provider.mapper.GatewayAuthRorMapper;
import com.starmark.gateway.auth.provider.service.IGatewayAuthResourceService;
import com.starmark.gateway.auth.provider.service.IGatewayAuthRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

@Component
@Order(value = 3)
public class GatewayAuthInitCommand implements CommandLineRunner {

    @Autowired
    IGatewayAuthRoleService sysAuthRoleService;

    @Resource
    GatewayAuthRorMapper gatewayAuthRorMapper;
    @Autowired
    private IGatewayAuthRoleService gatewayAuthRoleService;

    @Autowired
    private IGatewayAuthResourceService gatewayAuthResourceService;

    @Override
    @Transactional
    public void run(String... args) {
        {
            GatewayAuthRole admin = sysAuthRoleService.getById(GatewayAuthApiConstant.ADMIN_ROLE_ID);
            if (admin == null) {
                admin = new GatewayAuthRole();
                admin.setCode("SuperAdmin");
                admin.setAdmin(true);
                admin.setName("超级管理员");
                admin.setProjectCode("test");
                admin.setDescription("拥有该角色的人员可以有整个系统的所有权限");
                admin.setId(GatewayAuthApiConstant.ADMIN_ROLE_ID);
                sysAuthRoleService.save(admin);
                //插入相关关系
                GatewayAuthRor ror = new GatewayAuthRor();
                ror.setOrgId("1");
                ror.setRoleId(GatewayAuthApiConstant.ADMIN_ROLE_ID);
                ror.setOrgType(0x2);
                ror.setOrgName("超级管理员");
                gatewayAuthRorMapper.insert(ror);
            }
        }

        gatewayAuthRoleService.overload();
        gatewayAuthResourceService.overload();

    }
}
